using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace Acms.SqlDal
{
    public class UserDal : Base
    {
        public UserDal() : base() { }

        public enum Fields
        {
            Id,
            FirstName,
            LastName,
            DateOfBirth,
            EmailAddress
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<string>(Fields.FirstName, reader, table);
            GetReaderValue<string>(Fields.LastName, reader, table);
            GetReaderValue<DateTime>(Fields.DateOfBirth, reader, table);
            GetReaderValue<string>(Fields.EmailAddress, reader, table);
        }

        public System.Collections.Hashtable[] GetAll()
        {
            SqlCommand cmd = CreateCommand("GetAll");
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByUserName(string userName)
        {
            SqlCommand cmd = CreateCommand("GetByUserName");
            AddInputParameter(cmd, "UserName", userName, SqlDbType.VarChar, 50);
            return ExecuteGetItem(cmd);
        }

        public Hashtable GetByEmailAddress(string emailAddress)
        {
            SqlCommand cmd = CreateCommand("GetByEmailAddress");
            AddInputParameter(cmd, "EmailAddress", emailAddress, SqlDbType.VarChar, 250);
            return ExecuteGetItem(cmd);
        }

        public void SaveChanges(ref int id, string firstName, string lastName, DateTime dateOfBirth, string emailAddress, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "FirstName", firstName, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "LastName", lastName, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "DateOfBirth", dateOfBirth, SqlDbType.DateTime);
            AddInputParameter(cmd, "EmailAddress", emailAddress, SqlDbType.VarChar, 100);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }

    }
}
